#include				<SFML/Graphics.hpp>
#include				<iostream>
#include				"ImgManager.hpp"
#include                                "Animation.hpp"
#include                                "GameObject.hpp"
#include                                "Enums.hpp"
#include "AnimationPool.hpp"
#include				<list>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
    sf::View ViewBack(sf::FloatRect(0, 0, 800, 600));
    ImgManager::createImgManager();
    std::list<sf::Sprite*> listFire;
    int speed(200);
    int i = 0;

    sf::Sprite Back(ImgManager::getImage("back"));
    sf::Sprite Fire(ImgManager::getImage("fireEnd"));
    sf::Sprite Sprite(ImgManager::getImage("mob1"));
    Sprite.SetPosition(300, 200);
    Sprite.SetScale(2, 2);

    Animation anim2("img/explosion32.png", 32, 32, false, 20);
    Animation anim("img/r-typesheet7a.png", 32, 32, true, 40);
    GameObject lol(0, Player, 0, 1, 100, 100, anim, anim2);

    AnimationPool caca;

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if (Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Escape))
                App.Close();
            if (Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Space))
                //Fire.SetPosition(Sprite.GetPosition().x + 100, Sprite.GetPosition().y + 32);
                listFire.push_back(new sf::Sprite(ImgManager::getImage("fireEnd"), sf::Vector2f(Sprite.GetPosition().x + 100, Sprite.GetPosition().y + 32)));
            if (Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::A))
                listFire.push_back(new sf::Sprite(ImgManager::getImage("fireEnd"), sf::Vector2f(Sprite.GetPosition().x + 100, Sprite.GetPosition().y + 32)));
            if (Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::P))
                listFire.push_back(new sf::Sprite(ImgManager::getImage("fireEnd"), sf::Vector2f(Sprite.GetPosition().x + 100, Sprite.GetPosition().y + 32)));
        }

        float ElapsedTime = App.GetFrameTime();

        if (++i == 300)
            Sprite.SetImage(ImgManager::getImage("mob1"));
        else if (i == 600)
        {
            Sprite.SetImage(ImgManager::getImage("mob2"));
            i = 0;
        }

        if (App.GetInput().IsKeyDown(sf::Key::Left))
            Sprite.Move(-speed * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Right))
            Sprite.Move(speed * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Up))
            Sprite.Move(0, -speed * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Down))
            Sprite.Move(0, speed * ElapsedTime);

        if (Sprite.GetPosition().x + Sprite.GetSize().x > 800)
            Sprite.SetPosition(800 - Sprite.GetSize().x, Sprite.GetPosition().y);
        if (Sprite.GetPosition().y + Sprite.GetSize().y > 600)
            Sprite.SetPosition(Sprite.GetPosition().x, 600 - Sprite.GetSize().y);
        if (Sprite.GetPosition().x < 0)
            Sprite.SetPosition(0, Sprite.GetPosition().y);
        if (Sprite.GetPosition().y < 0)
            Sprite.SetPosition(Sprite.GetPosition().x, 0);

        /*
                ViewBack.Move(75 * ElapsedTime, 0);
                sf::Rect		temp(ViewBack.GetRect());

                if (ViewBack)
         */


        std::list<sf::Sprite*>::iterator itb = listFire.begin();
        std::list<sf::Sprite*>::iterator ite = listFire.end();

        for (; itb != ite; ++itb)
            if ((*itb)->GetPosition().y < 850)
                (*itb)->Move(250 * ElapsedTime, 0);

        Back.Move(-75 * ElapsedTime, 0);
        if (-Back.GetPosition().x > Back.GetSize().x / 2)
            Back.Move(-Back.GetPosition().x, 0);

        App.Clear();
        App.SetView(ViewBack);
        App.Draw(Back);
        App.SetView(App.GetDefaultView());
        App.Draw(Sprite);

        itb = listFire.begin();
        for (; itb != ite; ++itb)
            App.Draw(*(*itb));
        //App.Draw(anim.getFrame());
        App.Draw(lol.getRepresentation());
        App.Display();
    }

    ImgManager::delImgManager();
    return (EXIT_SUCCESS);
}
